package Singleton.type7;

/**
 * @Description:    单例模式 ------ 静态内部类
 * @Author: 		ZhaoLiangLiang
 * @date 			2023/2/13 09:23
 **/

public class Singleton07 {
    public static void main(String[] args) {
        Singleton test = Singleton.getInstance();
        Singleton test2 = Singleton.getInstance();
        System.out.println(test.hashCode());
        System.out.println(test2.hashCode());
        System.out.println(test == test2);
    }
}
class Singleton {
    // 1、构造器私有化 防止外部new
    private Singleton(){}

    // 2、本类内部创建对象实例
    private static Singleton instance;

    // 3、写一个静态内部类 该类中有一个静态属性 Singleton
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }

    // 4、提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
    public static synchronized Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}
